package com.example.ck.controller;

import com.example.ck.entity.Newproject;
import com.example.ck.service.NewprojectService;
import com.example.ck.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * 新建项目表(Newproject)表控制层
 *
 * @author makejava
 * @since 2024-02-27 21:57:36
 */
@Slf4j
@RestController
@RequestMapping("newproject")
@CrossOrigin
public class NewprojectController {
    @Autowired
    private NewprojectService newprojectService;

    /**
     * 新建项目申请
     */
    @PostMapping("/createProject")
    public ResultVo CreateProject(@RequestBody Newproject newproject) {
        log.info("新项目申请信息:{}", newproject);
        boolean flag = newprojectService.CreateProject(newproject);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("新项目申请成功");
            vo.setSuccess(true);
            vo.setData(newproject);
            vo.setCode(200);
        } else {
            vo.setMessage("新项目申请失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 上传附件 -> 实验建设材料
     */
    @PostMapping("/upload")
    public ResultVo uploadFile(MultipartFile file) throws IOException {
        // 将文件数据转换为字节数组
        byte[] fileData = file.getBytes();
        return new ResultVo(fileData);
    }

    /**
     * 根据当前登录的用户名查询所有创建的项目
     * 普通用户
     */
    @GetMapping("/query")
    public ResultVo queryAllProjectByUsername(String username) {
        List<Newproject> newprojects = newprojectService.queryProject(username);
        return new ResultVo(newprojects);
    }

    /**
     * 普通用户根据项目id来修改项目状态 -> 1 已提交
     */
    @PutMapping("/updateProjectStatus")
    public ResultVo updateProjectStatus(int id) {
        boolean b = newprojectService.submitProject(id);
        ResultVo vo = new ResultVo<>();
        if (b) {
            vo.setMessage("新项目提交成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("新项目提交失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 查询所有创建的项目
     */
    @GetMapping("/queryAll")
    public ResultVo queryAllProject() {
        List<Newproject> newprojects = newprojectService.queryAllProject();
        return new ResultVo(newprojects);
    }

    /**
     * 查询所有待审核的项目
     */
    @GetMapping("/queryAllToAudit")
    public ResultVo queryAllToAudit() {
        List<Newproject> newprojects = newprojectService.queryAllProjectByState();
        return new ResultVo(newprojects);
    }

    /**
     * 根据id移除项目申请
     */
    @DeleteMapping("remove")
    public ResultVo removeProject(@RequestParam Integer id) {
        boolean flag = newprojectService.RemoveProject(id);
        ResultVo vo = new ResultVo<>();
        if (flag) {
            vo.setMessage("项目申报移除成功");
            vo.setSuccess(true);
            vo.setCode(200);
        } else {
            vo.setMessage("项目申报移除失败");
            vo.setSuccess(false);
            vo.setCode(201);
        }
        return vo;
    }

    /**
     * 根据id查询项目信息
     */
    @GetMapping("findInfo")
    public ResultVo queryProjectById(@RequestParam Integer id) {
        Newproject newproject = newprojectService.queryProjectById(id);
        return new ResultVo(newproject);
    }
}

